<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
More control structures
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial018.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial020.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc39">3.7</A>&nbsp;&nbsp;More control structures</H2><UL>
<LI><A HREF="tutorial019.html#toc22">Disjunction</A>
<LI><A HREF="tutorial019.html#toc23">Conditional</A>
<LI><A HREF="tutorial019.html#toc24">Call</A>
<LI><A HREF="tutorial019.html#toc25">All Solutions</A>
</UL>

 <A NAME="toc22"></A>
<H3 CLASS="subsection"><A NAME="htoc40">3.7.1</A>&nbsp;&nbsp;Disjunction</H3>
<A NAME="@default71"></A> <A NAME="@default72"></A>
Disjunction is normally specified in Prolog by different clauses of
a predicate, but it can also be specified within a single clause by
the use of <CODE>;/2</CODE>. For example,<BR>
<BR>

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
atomic_particle(X) :- (X = proton ; X = neutron ; X = electron).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
This is logically equivalent to: <BR>
<BR>

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
atomic_particle(proton).
atomic_particle(neutron).
atomic_particle(electron).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<A NAME="toc23"></A>
<H3 CLASS="subsection"><A NAME="htoc41">3.7.2</A>&nbsp;&nbsp;Conditional</H3>
<A NAME="@default73"></A> <A NAME="@default74"></A>
Conditionals can be specified using the <CODE>-&gt;/2</CODE> operator.
In combination with <CODE>;/2</CODE>, a conditional similar to `if-then-else' 
constructs of conventional language can be constructed:
<CODE>X-&gt;Y;Z</CODE>, where <CODE>X</CODE>, <CODE>Y</CODE> and <CODE>Z</CODE> can be one or more
goals, means that if <CODE>X</CODE> is true, then <CODE>Y</CODE> will be
executed, otherwise <CODE>Z</CODE>. Only the first solution of <CODE>X</CODE> is
explored, so that on backtracking, no new solutions for <CODE>X</CODE> will be
tried. In addition, if <CODE>X</CODE> succeeds, then the `else' part, <CODE>Z</CODE>
will never be tried. If <CODE>X</CODE> fails, then the `then' part, <CODE>Y</CODE>,
will never be tried. An example of `if-then-else' is:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
max(X,Y, Max) :- 
   number(X), number(Y),
   (X &gt; Y -&gt; Max = X ; Max = Y).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
where <CODE>Max</CODE> is the bigger of the numbers <CODE>X</CODE> or <CODE>Y</CODE>.
Note the use of the brackets to make the scope of the if-then-else
clear and correct.<BR>
<BR>
<A NAME="toc24"></A>
<H3 CLASS="subsection"><A NAME="htoc42">3.7.3</A>&nbsp;&nbsp;Call</H3> 
<A NAME="@default75"></A>
<A NAME="@default76"></A>
One feature of Prolog is the equivalence of programs and data &ndash; both are
represented as terms. The predicate <CODE>call</CODE> allows 
program terms (i.e. data) to be treated as goals: <CODE>call(X)</CODE> will cause
<CODE>X</CODE> to be treated as a goal and executed. Although at the time when 
the predicate is executed, <CODE>X</CODE> has to be instantiated, it does not 
need to be instantiated (or even known) at compile time. For example, it
would in principle be
possible to define disjunction (<CODE>;</CODE>) as follows:<BR>
<BR>

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
X ; Y :- call(X).
X ; Y :- call(Y).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<A NAME="toc25"></A>
<H3 CLASS="subsection"><A NAME="htoc43">3.7.4</A>&nbsp;&nbsp;All Solutions</H3>
<A NAME="all-solutions"></A>
In the pure computational model of Prolog, alternative solutions are
computed one-by-one on backtracking. Only one solution is available
at any time, while previous solutions disappear on backtracking:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- weekday(X).
X = mo
More
X = tu
More
X = we
More
...
</PRE></BLOCKQUOTE>
Sometimes it is useful to have all solution together in a list.
This can be achieved by using one of the all-solutions predicates
<A NAME="@default77"></A><A NAME="@default78"></A><A NAME="@default79"></A>findall/3, setof/3 or bagof/3:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- findall(X, weekday(X), List).
X = X
List = [mo, tu, we, th, fr, sa, su]
Yes
</PRE></BLOCKQUOTE>

<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For the differences between findall/3, setof/3 and bagof/3
see the ECL</FONT><SUP><FONT COLOR="#9832CC"><I>i</I></FONT></SUP><FONT COLOR="#9832CC">PS</FONT><SUP><FONT COLOR="#9832CC"><I>e</I></FONT></SUP><FONT COLOR="#9832CC"> Reference Manual.</FONT>
</DL>

<HR>
<A HREF="tutorial018.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial020.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
